import { ListenerFunTypeEnum } from './terminalTypes';

const poop = {};

/**
 * @description 注册方法
 */
function register(name, listener) {
  if (poop[name]) {
    throw Error(`Unable to register an existing terminal: ${name}`)
  }
  poop[name] = listener
}

/**
 * @description 销毁方法
 */
function unregister(name) {
  delete poop[name]
}

const TerminalApi = {
  /** 执行命令，启动方法体 */
  post(name = 'linpeng-web-terminal', event, options) {
    const listener = poop[name];
    if (listener) return listener(event, options)
  },
  /** 添加指令消息 */
  pushMessage(name, options) {
    return TerminalApi.post(name, ListenerFunTypeEnum.pushMessage, options)
  },
  /** 执行命令 */
  execute(name, options) {
    return TerminalApi.post(name, ListenerFunTypeEnum.execute, options);
  }
}
export default TerminalApi;

const {
  pushMessage,
  execute
} = TerminalApi;
export {
  register,
  unregister,
  pushMessage,
  execute
}
